package com.xbb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xbb.config.JwtConfig;
import com.xbb.entity.Role;
import com.xbb.entity.User;
import com.xbb.mapper.UserMapper;
import com.xbb.service.UserService;
import com.xbb.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前端发送登录请求的时候，服务端返回一个token；前端再次发送其他请求的时候，要携带token，服务端验证通过后才可以获取请求数据。
 */
@RestController
public class LoginController {

    @Resource
    private JwtConfig jwtConfig ;

    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;
    /*
     * 返参格式
     * {
     *     "userName": "1",
     *     "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMSIsImlhdCI6MTU4OTIwMTMzNCwiZXhwIjoxNTg5MjA0OTM0fQ.9RdZ7-yRnSUM_QfixRNVW5n3hr2XV-YOd3vg0nwRzdaXLbvLVPmkLe5ixsSM4cVNlBdcJ6Swmvnnw3A57frDzw"
     * }
     */
    // 拦截器直接放行登录请求，返回Token
    @PostMapping("/login")
    public Map<String,Object> login (@RequestParam("loginName") String loginName,
                                     @RequestParam("password") String password){
        Map<String,Object> result = new HashMap<>() ;
        String md5 = MD5.getInstance().getMD5(password);
        //数据源校验
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("login_name",loginName);
        QueryWrapper<User> queryWrapper2 = new QueryWrapper<User>();
        queryWrapper2.eq("login_name",loginName).eq("password",md5);
        //根据用户名和密码查询单个对象
        User one = userService.getOne(queryWrapper, false);
        User two = userService.getOne(queryWrapper2, false);

        String lock = userMapper.getLock(loginName);
        System.out.println(lock);
        if(lock.equals("是")){
            List<Role> list = userMapper.yanzhengGuanLiYuan(loginName);
            System.out.println(list);
            if(list.size()>0&&list.get(0).getName().contains("管理员")){
                userMapper.updateLockOut(loginName);
                String md51 = MD5.getInstance().getMD5("ysd123");
                one.setPassword(md51);
                userService.updatePassWord(one);
                userMapper.updateEmptyWrongNum(loginName);
                result.put("code", 504);
                result.put("msg", "由于密码3次错误，账号锁定，但由于您是管理员的身份,所以已为您重置密码,请用初始密码登录");
            }else{
                result.put("code", 503);
                result.put("msg", "由于密码3次错误，账号锁定，请联系管理员解锁");
            }
        }else if (one==null){
            result.put("code",501);
            result.put("msg","登录失败，用户名不存在");
        }else if(two==null){
            userService.updateWrongNum(loginName);
            Integer wrongNum = userMapper.getWrongNum(loginName);
            System.out.println("密码错误次数"+wrongNum);
            if(wrongNum>=3){
                userMapper.updateLock(loginName);
            }
            result.put("code",502);
            if(wrongNum<3){
                result.put("msg","密码错误,您的密码错误["+wrongNum+"]次,还有["+(3-wrongNum)+"]次机会呦");
            }else{
                result.put("msg","密码错误,您的密码已经错误["+wrongNum+"]次,账号已锁定");
            }
        }else {
            //登录成功之后
            userMapper.updateLockOut(loginName);
            userMapper.updateEmptyWrongNum(loginName);
            String token = jwtConfig.getToken(loginName+password) ;
            if (!StringUtils.isEmpty(token)) {
                result.put("token",token) ;
            }
            result.put("code",0);
            System.out.println(one.getImgUrl());
            result.put("data",one);
        }

        return result ;
    }
    // 需要 Token 验证的接口
    @PostMapping("/info")
    public String info (){
        return "info" ;
    }
}
